<template>
  <div>
    <div class="filter-container queryTool">
      <el-input v-model="listQuery.name" placeholder="数据库表名" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <!--      <el-input v-model="listQuery.anotherName" placeholder="别名" style="width: 200px;" class="filter-item"-->
      <!--                @keyup.enter.native="handleFilter"/>-->

      <!--      <el-date-picker v-model="timeQuery" type="datetimerange" range-separator="至"  start-placeholder="开始日期" end-placeholder="结束日期" />-->
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width:100%;"
    >
      <el-table-column prop="id" label="ID" width="180">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="false" prop="fileTableId" label="文件表id" width="180">
        <template slot-scope="{row}">
          <span>{{ row.fileTableId }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="fileName" label="文件名" />
      <el-table-column prop="name" label="数据库表名" />
      <el-table-column prop="startTime" label="开始时间" :formatter="dateFormatter" />
      <el-table-column prop="endTime" label="结束时间" :formatter="dateFormatter" />
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button size="mini" type="danger" @click="deleteDate(row,$index)">
            删除
          </el-button>
          <el-button type="primary" size="el-icon-view" @click="lookData(row)">
            查看数据
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { deleteDate, fetchArticle } from '@/api/article'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import waves from '@/directive/waves' // waves directive
import store from '@/store'
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        name: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      fetchArticle(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        // this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      /* if (this.timeQuery !== null) {
        this.listQuery.startTime = this.timeQuery[0].getTime()
        this.listQuery.endTime = this.timeQuery[1].getTime()
      } else {
        this.listQuery.startTime = null
        this.listQuery.endTime = null
      }*/
      this.getList()
    },
    deleteDate(row, index) {
      deleteDate({ id: row.id }).then(response => {
        this.$notify({
          title: response.title,
          message: response.message,
          type: response.type,
          duration: 2000
        })
        this.getList()
      })
    },
    async lookData(row) {
      console.log(row)
      await store.dispatch('data/setDataLoadInfo', row)
      this.$router.replace({
        path: '/chart/lineChart'
      })
    },
    dateFormatter(row, column) {
      let datetime = row[column.property]
      if (datetime) {
        datetime = new Date(datetime)
        const year = datetime.getFullYear()
        const month = datetime.getMonth() + 1
        const day = datetime.getDate()
        const hours = datetime.getHours()
        const minutes = datetime.getMinutes()
        const seconds = datetime.getSeconds()
        return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
      }
      return ''
    }

  }
}
</script>

<style lang="scss">
.queryTool {
  margin-bottom: 10px;
}
</style>

